﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;
using MSL.DataBase.Lib;
using System.Collections;
using MSL.DataBase.SQL;

namespace MSL.DataBase.Core
{
    internal class OleHelper : DbBase
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="conn">链接字符串</param>
        public OleHelper(string conn)
            : base(DataBaseType.Access, conn, DBConfig.OleDb)
        {

        }
        public OleHelper()
            : base(DataBaseType.Access, DBConfig.ConnString, DBConfig.OleDb)
        {

        }

        public override void AddReturnPara()
        {

        }
        public override System.Data.Common.DbParameter GetNewParameter()
        {
            return new OleDbParameter();
        }
        public override void AddParameters(string parameterName, object value, DbType dataType, int size, ParameterDirection direction)
        {
            parameterName = parameterName.Substring(0, 1) == "@" ? parameterName : "@" + parameterName;
            OleDbParameter para = new OleDbParameter();
            para.ParameterName = parameterName;
            para.Value = value;
            if (dataType == DbType.DateTime)
            {
                para.OleDbType = OleDbType.DBTimeStamp;
                para.Value = Convert.ToString(value);
            }
            else
            {
                para.DbType = dataType;
                para.Value = value;
            }
            Com.Parameters.Add(para);
        }

        #region 分页查询

        public override DataTable Query(string tableName, string primaryKey, string showFields, int pageIndex, int pageSize, string where, string sortBy, string groupBy, out int recordCount)
        {
            recordCount = 0;
            DataTable dt = null;
            Hashtable parameters = new Hashtable();

            string sql = SQLString.OledbPage(pageIndex, pageSize, where, tableName, primaryKey, showFields, sortBy, recordCount, true);
            recordCount = ExecScalar<Int32>(sql, false);
            if (recordCount > 0 && (pageIndex - 1) * pageSize < recordCount)
            {
                sql = SQLString.OledbPage(pageIndex, pageSize, where, tableName, primaryKey, showFields, sortBy, recordCount, false);
                dt = Query(sql, false);
            }
            base._com.Parameters.Clear();
            return dt;
        }

        #endregion

        #region 数据库管理

        /// <summary>
        /// 还原数据库
        /// </summary>
        /// <param name="dbName">数据库名</param>
        /// <param name="backfile">备份文件</param>
        /// <returns>还原成功与否提示</returns>
        public override bool RestoreDatabase(string dbName, string backfile)
        {
            return false;
        }

        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <param name="dbName">数据库名</param>
        /// <param name="dbFileName">备份文件</param>
        /// <returns>是否备份成功</returns>
        public override bool BackDataBase(string dbName, string dbFileName)
        {
            return false;
        }

        /// <summary>
        /// 新建数据库
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        public override bool CreateDataBase(string databaseName)
        {
            return false;
        }

        /// <summary>
        /// 删除数据库
        /// </summary>
        public override bool DropDataBase(string databaseName)
        {
            return false;
        }
        #endregion
    }
}